package com.qf.czh.aop;

import com.qf.czh.mapper.LogMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Aspect
@Component
@EnableAspectJAutoProxy()
public class LogAspect {
    @Autowired
    LogMapper logMapper;

    @Before("execution(*  com.qf.czh.controller.LoginController.dologin(..))")
    public void loginLog(JoinPoint joinPoint){
        System.out.println("前置通知LLoginController");
        Object[] args = joinPoint.getArgs();
        System.out.println(args);
        String[] parameterNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
        System.out.println(Arrays.toString(parameterNames));
    }
//
    @Before("execution(* com.qf.czh.controller.LeaveController.leaveData(..))")
    public void loginLog1(JoinPoint joinPoint){
        System.out.println("前置通知LeaveController");
    }

    @Before("execution(* com.qf.czh.service.LeaveService.getLeaveByPage(..))")
    public void loginLog2(JoinPoint joinPoint){
        System.out.println("前置通知LeaveService");
    }

    @After("execution(* com.qf.czh.controller.LoginController.dologin(..))")
    public void loginLogAfter(JoinPoint joinPoint){
        System.out.println("后置通知");
        Subject subject = SecurityUtils.getSubject();
        Object principal = subject.getPrincipal();
        System.out.println(principal);
        String logintime = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
        Map<String , Object> params = new HashMap<String, Object>();
        params.put("username", principal);
        params.put("logintime" , logintime);
        params.put("type",1);
        logMapper.addLoginLog(params);

    }

//    @AfterReturning(value = "execution(* com.qf.czh.controller.LoginController.dologin(..))" , returning = "RespBean")
//    public Object login1(Object o){
//        return null;
//    }




    @Pointcut("execution(* org.apache.shiro.mgt.DefaultSecurityManager.logout(..))")
    public void pc1(){};

    @Before("pc1()")
    public void loginLogBefore(JoinPoint joinPoint){
        System.out.println("logout");

        Subject subject = SecurityUtils.getSubject();
        Object principal = subject.getPrincipal();
        System.out.println(principal);
        String logintime = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
        Map<String , Object> params = new HashMap<String, Object>();
        params.put("username", principal);
        params.put("logintime" , logintime);
        params.put("type",2);
        logMapper.addLoginLog(params);
    }
}
